package com.lxt.controller.center;

import com.lxt.common.HttpResult;
import com.lxt.common.PagedGridResult;
import com.lxt.controller.BaseController;
import com.lxt.pojo.Orders;
import com.lxt.pojo.VO.OrderStatusCountsVO;
import com.lxt.service.center.MyOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author kevin.lv
 * @date 2022/8/9 20:29
 */
@Api(value = "用户中心我的订单", tags = {"用户中心我的订单"})
@RestController
@RequestMapping("myorders")
public class MyOrdersController extends BaseController {

    @Autowired
    private MyOrdersService myOrdersService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/query")
    public HttpResult queryPagedComments(
            @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false) @RequestParam Integer orderStatus,
            @ApiParam(name = "page", value = "查询第几页", required = false) @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false) @RequestParam Integer pageSize) {
        if (userId == null) {
            return HttpResult.errorMsg("用户ID不存在");
        }
        if (page == null) {
            page = COMMENT_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize);
        return HttpResult.ok(result);
    }

    //模拟商家发货
    @ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public HttpResult deliver(
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam String orderId) {

        if (StringUtils.isBlank(orderId)) {
            return HttpResult.errorMsg("订单Id不能为空");
        }
        myOrdersService.updateDeliverOrderStatus(orderId);
        return HttpResult.ok();
    }

    @ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public HttpResult confirmReceive(
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {

        HttpResult checkResult = checkUserOrder(orderId, userId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        boolean result = myOrdersService.updateReceiveOrderStatus(orderId);
        if (!result) {
            return HttpResult.errorMsg("订单确认收货失败!");
        }

        return HttpResult.ok();
    }

    @ApiOperation(value = "用户删除订单", notes = "用户删除订单", httpMethod = "POST")
    @PostMapping("/delete")
    public HttpResult delete(
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {

        if (StringUtils.isBlank(orderId)) {
            return HttpResult.errorMsg("订单Id不能为空");
        }
        HttpResult checkResult = checkUserOrder(orderId, userId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        boolean result = myOrdersService.deleteOrder(userId, orderId);
        if (!result) {
            return HttpResult.errorMsg("删除订单失败!");
        }
        return HttpResult.ok();
    }

    @ApiOperation(value = "查询订单状态概况", notes = "查询订单状态概况", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public HttpResult statusCounts(
            @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) {
        if (userId == null) {
            return HttpResult.errorMsg("用户ID不存在");
        }
        OrderStatusCountsVO orderStatusCounts = myOrdersService.getOrderStatusCounts(userId);
        return HttpResult.ok(orderStatusCounts);
    }

    @ApiOperation(value = "查询订单动向", notes = "查询订单动向", httpMethod = "POST")
    @PostMapping("/trend")
    public HttpResult trend(
            @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(name = "page", value = "查询第几页", required = false) @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false) @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return HttpResult.errorMsg("用户ID不存在");
        }
        if (page == null) {
            page = COMMENT_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = myOrdersService.getMyOrderTrend(userId, page, pageSize);
        return HttpResult.ok(result);
    }

}
